package com.zp.qualitypractice.list;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by izp on 2017/4/20.
 */

public class CustomCircleView extends View {
    private float mWidthHeight = 0f;
    private float mCircleRadius = 0f;//半径
    private float mCircleWidth = 5f;//圆的宽度

    private boolean isInit = false;//是否已经初始化的一个标识

    private Circle mOuterCircle;
    private Circle mPercentCircle;


    private float mCurrentPercent = 0.75f;

    public CustomCircleView(Context context) {
        super(context);
    }

    public CustomCircleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomCircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public CustomCircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        init();
        mOuterCircle.draw(canvas, 1f);
        mPercentCircle.draw(canvas, mCurrentPercent);

        super.onDraw(canvas);
    }
    public void update(float percent) {
        mCurrentPercent = percent > 1 ? 1 : percent;
        invalidate();//会重新调用draw
    }

    private void init() {
        if (!isInit) {//如果之前没有初始化就初始化,主要是构建两个图形.
            isInit = true;
            mWidthHeight = Math.min(getMeasuredWidth(), getMeasuredHeight());
            mCircleRadius = mWidthHeight * 3f / 10f;//圆的半径是高的10分之3，
            mOuterCircle = new Circle(mCircleRadius, mWidthHeight, mCircleWidth, Color.LTGRAY);
            mPercentCircle = new Circle(mCircleRadius, mWidthHeight, mCircleWidth, Color.GRAY);
        }
    }

    private class Circle {
        Paint mPaint;
        RectF mRectF;

        /**
         * @param radius 半径
         * @param widthHeight 宽高,这个宽高是指的什么
         * @param circleWidth 圆的宽,有半径了为什么还要宽? 难道是环的宽?
         * @param color
         */
        public Circle(float radius, float widthHeight, float circleWidth, int color) {
            mPaint = new Paint();//初始化画笔
            mPaint.reset();//为毛刚初始化就要重置?
            mPaint.setStyle(Paint.Style.STROKE);//风格是描边,有填充
            mPaint.setStrokeWidth(circleWidth);//设置描边宽度,就是paint的外宽度
            mPaint.setAntiAlias(true);//开启抗锯齿
            mPaint.setColor(color);

            float leftPadding = widthHeight/2f - radius - circleWidth;//图形距离View左边的距离
            //view的宽高 - 半径-描边的宽度
            float topPadding = leftPadding;
            float rightPadding = widthHeight - leftPadding;
            float bottomPadding = widthHeight - topPadding;

            this.mRectF = new RectF( leftPadding, topPadding, rightPadding, bottomPadding);//绘制一个矩形,padding于外层View容器?

        }

        /**
         * 它操作canvas,而并不输出,只更改canvas的状态
         * @param canvas
         * @param percent
         */
        public void draw(Canvas canvas, float percent) {//画圆弧
            canvas.drawArc(mRectF, 0, 360 * percent, false, mPaint);//从-90为起点,到指定的percent结束
        }

    }

}
